<?php

namespace gogrpc;

use Proto\LiveServerClient;

class LiveServer extends BaseServer
{

    protected static $client   = null;
    protected static $metadata = [];

    public function __construct()
    {
        // 日志文件
        parent::$log_file = 'live_grpc.log';

        self::$metadata = [
            "app_id"     => [config('grpc.app_id')],
            "app_secret" => [config('grpc.app_secret')],
        ];

        if (!self::$client) {
            self::$client = new LiveServerClient(config('grpc.live_sever'), [
                'credentials' => \Grpc\ChannelCredentials::createInsecure(),
            ]);
        }
    }

    /**
     * 结束直播
     * @param $uid
     * @return bool
     */
    public function endLive($anchor_id)
    {
        $request = new \Proto\EndLiveReq();
        $request->setAnchorId($anchor_id);

        list($response, $status) = self::$client->EndLive($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }


    /**
     * 关键词黑名单屏蔽
     * @param $words 关键词
     * @param int $type 0=删除  1增加
     * @return bool
     */
    public function addSensitive($words, $type = 1)
    {
        if (is_array($words)) {
            $words = implode(',', $words);
        }
        $request = new \Proto\AddSensitiveReq();
        $request->setOpType($type);
        $request->setWords($words);

        list($response, $status) = self::$client->AddSensitive($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }


    /**
     * 清理聊天记录
     * @param $uid
     * @return bool
     */
    public function clearChatAdmin($chat_id, $user_id, $room_id)
    {
        $request = new \Proto\ClearChatReq();
        $request->setChatId($chat_id);
        $request->setUserId($user_id);
        $request->setRoomId($room_id);
        list($response, $status) = self::$client->ClearChatAdmin($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    /**
     * 禁言用户聊天室清屏消息
     * @param $user_id 用户id
     * @param $nick_name 用户昵称
     * @param $room_id 房间id
     * @param $ban_type 1禁言, 2解除禁言
     * @param $t_ban 禁言时长，以秒为单位，永久禁言传值：4294967295
     * @param $reason 禁言原因
     * @return bool
     */
    public function banUserAllChatAdmin($user_id, $nick_name, $room_id, $ban_type, $t_ban, $reason = '')
    {
        $request = new \Proto\BanUserChatReq();
        $request->setUserId($user_id);
        $request->setNickname($nick_name);
        $request->setRoomId($room_id);
        $request->setBanType($ban_type);
        $request->setTBan($t_ban);
        $request->setReason($reason);

        list($response, $status) = self::$client->BanUserAllChatAdmin($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    /**
     * 修改直播间热度
     * @param $anchor_id 主播id
     * @param $heat 热度值
     * @return bool
     */
    public function UpdateBaseHeat($anchor_id, $heat)
    {
        $request = new \Proto\UpdateBaseHeatReq();
        $request->setAnchorid($anchor_id);
        $request->setHeat($heat);

        list($response, $status) = self::$client->UpdateBaseHeat($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    /**
     * 直播间公告推送
     * @param $content 内容
     * @param $contact 联系方式 json字符串
     * @param $room_id 房间id
     * @return bool
     */
    public function roomBoardcast($room_id, $content, $contact)
    {
        $request = new \Proto\RoomBoardcastReq();
        $request->setType('announcement');
        $request->setRoomId($room_id);
        $request->setContent($content);
        $request->setContact($contact);

        list($response, $status) = self::$client->RoomBoardcast($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    // 直播间发送后台管理员消息
    public function adminPub($room_id, $content, $name)
    {
        $request = new \Proto\AdminPubReq();
        $request->setRoomId($room_id);
        $request->setContent($content);
        $request->setName($name);

        list($response, $res) = self::$client->AdminPub($request, self::$metadata)->wait();

        if ($res->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $res);
        return false;
    }

    // 回放一键开播
    public function adminReplayToLive($id)
    {
        $request = new \Proto\AdminPlaybackToLiveReq();
        $request->setId($id);

        list($response, $res) = self::$client->AdminPlaybackToLive($request, self::$metadata)->wait();

        if ($res->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $res);
        return false;
    }
}
